/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import Client.domain.ClientItem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

/**
 *
 * @author Ripper
 */
public class PlayerInventoryPanel extends ClientPanel implements ActionListener, MouseListener
{
    private JPanel equippedItemsPanel;
    private JPanel inventoryPanel;
    
    private JButton returnButton;
    
    private ItemLabel ilNecklace;
    private ItemLabel ilHelmet;
    private ItemLabel ilLeftWeap;
    private ItemLabel ilChestArm;
    private ItemLabel ilRightWeap;
    private ItemLabel ilGloves;
    private ItemLabel ilLeggings;
    private ItemLabel ilBelt;
    private ItemLabel ilLeftRing;
    private ItemLabel ilBoots;
    private ItemLabel ilRightRing;
    
    private JLabel inspectNameLabel;
    private JLabel inspectImageLabel;
    private JLabel inspectTypeLabel;
    private JTextArea inspectAttributesArea;
    
    private List<ClientItem> items;
    private int itemPage = 0;
    
    private int pageXSize = 6;
    private int pageYSize = 4;
    private ItemLabel[][] inventoryItems;
    
    private JButton scrollLeftIntoInventory;
    private JButton scrollRightIntoInventory;
    
    private int firstClickIndex = -1;
    private long firstClickTime = System.currentTimeMillis();
    
    public PlayerInventoryPanel(ClientWindow cw)
    {
        super(cw);
        setLayout(new BorderLayout());
        
        JPanel westernPanel = new JPanel(new BorderLayout());
        {
            equippedItemsPanel = new JPanel(new BorderLayout()); //TODO
            equippedItemsPanel.setBorder(BorderFactory.createTitledBorder("Equipped Items"));
            {
                JPanel innerPanel = new JPanel(new GridLayout(4,3));
                {
                    //row 1: necklace, helmet, blank
                    JPanel tinyNecklacePanel = new JPanel(new BorderLayout());
                    {
                        ilNecklace = new ItemLabel();
                        ilNecklace.addMouseListener(this);
                        tinyNecklacePanel.add(ilNecklace, BorderLayout.CENTER);
                        tinyNecklacePanel.add(new JLabel("Necklace:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyNecklacePanel);
                    JPanel tinyHelmetPanel = new JPanel(new BorderLayout());
                    {
                        ilHelmet = new ItemLabel();
                        ilHelmet.addMouseListener(this);
                        tinyHelmetPanel.add(ilHelmet, BorderLayout.CENTER);
                        tinyHelmetPanel.add(new JLabel("Helmet:"), BorderLayout.NORTH);
                    }   
                    innerPanel.add(tinyHelmetPanel);
                    innerPanel.add(new JLabel(""));
                    
                    //row 2: left weapon, chestarmor, right weapon
                    JPanel tinyLeftWeaponPanel = new JPanel(new BorderLayout());
                    {
                        ilLeftWeap = new ItemLabel();
                        ilLeftWeap.addMouseListener(this);
                        tinyLeftWeaponPanel.add(ilLeftWeap, BorderLayout.CENTER);
                        tinyLeftWeaponPanel.add(new JLabel("Left Hand:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyLeftWeaponPanel);
                    JPanel tinyChestArmorPanel = new JPanel(new BorderLayout());
                    {
                        ilChestArm = new ItemLabel();
                        ilChestArm.addMouseListener(this);
                        tinyChestArmorPanel.add(ilChestArm, BorderLayout.CENTER);
                        tinyChestArmorPanel.add(new JLabel("Chest Armor:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyChestArmorPanel);
                    JPanel tinyRightWeaponPanel = new JPanel(new BorderLayout());
                    {
                        ilRightWeap = new ItemLabel();
                        ilRightWeap.addMouseListener(this);
                        tinyRightWeaponPanel.add(ilRightWeap, BorderLayout.CENTER);
                        tinyRightWeaponPanel.add(new JLabel("Right Hand:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyRightWeaponPanel);
                    
                    //row 3: gloves, leggings, belt
                    JPanel tinyGlovePanel = new JPanel(new BorderLayout());
                    {
                        ilGloves = new ItemLabel();
                        ilGloves.addMouseListener(this);
                        tinyGlovePanel.add(ilGloves, BorderLayout.CENTER);
                        tinyGlovePanel.add(new JLabel("Gloves:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyGlovePanel);
                    JPanel tinyLeggingsPanel = new JPanel(new BorderLayout());
                    {
                        ilLeggings = new ItemLabel();
                        ilLeggings.addMouseListener(this);
                        tinyLeggingsPanel.add(ilLeggings, BorderLayout.CENTER);
                        tinyLeggingsPanel.add(new JLabel("Leggings:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyLeggingsPanel);
                    JPanel tinyBeltPanel = new JPanel(new BorderLayout());
                    {
                        ilBelt = new ItemLabel();
                        ilBelt.addMouseListener(this);
                        tinyBeltPanel.add(ilBelt, BorderLayout.CENTER);
                        tinyBeltPanel.add(new JLabel("Belt:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyBeltPanel);
                    
                    //row 4: left ring, boots, right ring
                    JPanel tinyLeftRing = new JPanel(new BorderLayout());
                    {
                        ilLeftRing = new ItemLabel();
                        ilLeftRing.addMouseListener(this);
                        tinyLeftRing.add(ilLeftRing, BorderLayout.CENTER);
                        tinyLeftRing.add(new JLabel("Ring:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyLeftRing);
                    JPanel tinyBootsPanel = new JPanel(new BorderLayout());
                    {
                        ilBoots = new ItemLabel();
                        ilBoots.addMouseListener(this);
                        tinyBootsPanel.add(ilBoots, BorderLayout.CENTER);
                        tinyBootsPanel.add(new JLabel("Boots:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyBootsPanel);
                    JPanel tinyRightring = new JPanel(new BorderLayout());
                    {
                        ilRightRing = new ItemLabel();
                        ilRightRing.addMouseListener(this);
                        tinyRightring.add(ilRightRing, BorderLayout.CENTER);
                        tinyRightring.add(new JLabel("Ring:"), BorderLayout.NORTH);
                    }
                    innerPanel.add(tinyRightring);
                }
                equippedItemsPanel.add(innerPanel, BorderLayout.NORTH);
                //TODO
            }
            westernPanel.add(equippedItemsPanel, BorderLayout.CENTER);
        }
        add(westernPanel, BorderLayout.WEST);
        
        JPanel middlePanel = new JPanel(new BorderLayout());
        {
            inventoryPanel = new JPanel(new GridLayout(pageYSize,pageXSize)); //TODO
            inventoryPanel.setBorder(BorderFactory.createTitledBorder("Inventory"));
            {
                inventoryItems = new ItemLabel[pageXSize][pageYSize];
                for(int a = 0; a < pageXSize; a++)
                {
                    for(int b = 0; b < pageYSize; b++)
                    {
                        inventoryItems[a][b] = new ItemLabel();
                        inventoryItems[a][b].addMouseListener(this);
                        inventoryPanel.add(inventoryItems[a][b]);
                    }
                }
            }
            middlePanel.add(inventoryPanel, BorderLayout.CENTER);
            
            JPanel buttonPanelz = new JPanel(new GridLayout(2,1));
            {
                returnButton = new JButton(" return ");
                returnButton.addActionListener(this);
                scrollLeftIntoInventory = new JButton("  <  ");
                scrollRightIntoInventory = new JButton("  >  ");
                scrollLeftIntoInventory.addActionListener(this);
                scrollRightIntoInventory.addActionListener(this);
                
                JPanel topBitInButtonPanelz = new JPanel(new GridLayout(1,2));
                {
                    topBitInButtonPanelz.add(scrollLeftIntoInventory);
                    topBitInButtonPanelz.add(scrollRightIntoInventory);
                }
                buttonPanelz.add(topBitInButtonPanelz);
                buttonPanelz.add(returnButton);
            }
            middlePanel.add(buttonPanelz, BorderLayout.SOUTH);
            
            JPanel itemDetailPanel = new JPanel(new BorderLayout());
            itemDetailPanel.setBorder(BorderFactory.createTitledBorder("Item Inspection"));
            {
                inspectNameLabel = new JLabel();
                inspectImageLabel = new JLabel();
                inspectTypeLabel = new JLabel();
                inspectAttributesArea = new JTextArea();
                inspectAttributesArea.setEditable(false);
                
                itemDetailPanel.add(inspectNameLabel, BorderLayout.NORTH);
                itemDetailPanel.add(inspectImageLabel, BorderLayout.CENTER);
                
                JPanel bottomBit = new JPanel(new BorderLayout());
                {
                    bottomBit.add(inspectTypeLabel, BorderLayout.NORTH);
                    bottomBit.add(inspectAttributesArea, BorderLayout.CENTER);
                }
                itemDetailPanel.add(bottomBit,BorderLayout.SOUTH);
            }
            middlePanel.add(itemDetailPanel, BorderLayout.EAST);
        }
        add(middlePanel, BorderLayout.CENTER);
        
        
    }
    
    public void refreshData()
    {
        String question = "actvplr_eqsummary";
        String data = getClientWindow().getClientDomainController().askTheServer(question);
        
        if(data.endsWith(";"))
        {
            data = data.substring(0, data.length() - 1);
        }
        
        String[] itanBits = data.split("-");
        
        int i = 0;
        for(String itan: itanBits)
        {
            ClientItem ci = null;
            /*if(itan.length() < 3)
            {
                i++;
                continue;
            }*/
            
            try
            {
                ci = new ClientItem(getClientWindow().getClientDomainController(), itan);
            }catch(Exception ex)
            {
                
            }
            
            switch(i)
            {
                case 0:
                    ilNecklace.setClientItem(ci);
                    break;
                case 1:
                    ilHelmet.setClientItem(ci);
                    break;
                case 2:
                    ilLeftWeap.setClientItem(ci);
                    break;
                case 3:
                    ilChestArm.setClientItem(ci);
                    break;
                case 4:
                    ilRightWeap.setClientItem(ci);
                    break;
                case 5:
                    ilGloves.setClientItem(ci);
                    break;
                case 6:
                    ilLeggings.setClientItem(ci);
                    break;
                case 7:
                    ilBelt.setClientItem(ci);
                    break;
                case 8:
                    ilLeftRing.setClientItem(ci);
                    break;
                case 9:
                    ilBoots.setClientItem(ci);
                    break;
                case 10:
                    ilRightRing.setClientItem(ci);
                    break;
                default:
                    break;
            }
            i++;
        }
        
        
        question = "actvplr_inventcontent";
        data = getClientWindow().getClientDomainController().askTheServer(question);
        if(data.endsWith(";"))
        {
            data = data.substring(0, data.length() - 1);
        }
        
        String[] inventoryItanBits = data.split("-");
        
        items = new ArrayList<ClientItem>();
        for(String s: inventoryItanBits)
        {
            try
            {
                ClientItem caai = new ClientItem(getClientWindow().getClientDomainController(), s);
                items.add(caai);
            }
            catch(Exception ex)
            {
                
            }
        }
        
        int currentItem = (pageXSize * pageYSize) * itemPage;
        for(int a = 0; a < pageXSize; a++)
        {
            for(int b = 0; b < pageYSize; b++)
            {
                ItemLabel il = inventoryItems[a][b];
                
                try
                {
                    il.setClientItem(items.get(currentItem));
                }
                catch(Exception ex){
                    il.setClientItem(null);
                }
                
                currentItem++;
            }
        }
    }
    
    public void inspect(ClientItem ci)
    {
        if(ci != null)
        {
            inspectNameLabel.setText("Name: "+ci.getItemName());
            inspectImageLabel.setIcon(new ImageIcon(ci.getImage()));
            inspectTypeLabel.setText("Type: "+ci.getItemType());
            inspectAttributesArea.setText("Attributes: \n"+ci.getItemDescription().replaceAll(", ", ",      \n")+"  ");
        }
        else
        {
            inspectNameLabel.setText("");
            inspectImageLabel.setIcon(null);
            inspectTypeLabel.setText("");
            inspectAttributesArea.setText("");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == returnButton)
        {
            getClientWindow().gotoWelcomeScreen();
        }
        else if(e.getSource() == scrollLeftIntoInventory)
        {
            if(itemPage > 0)
            {
                itemPage--;
                refreshData();
            }
        }
        else if(e.getSource() == scrollRightIntoInventory)
        {
            if(pageXSize*pageYSize*(itemPage+1) < items.size())
            {
                itemPage++;
                refreshData();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) 
    {
        int clickedIndex = 0;
        for(int a = 0; a < pageXSize; a++)
        {
            for(int b = 0; b < pageYSize; b++)
            {
                if(e.getSource() == inventoryItems[a][b])
                {
                    if(inventoryItems[a][b].getClientItem() != null)
                    {
                        if(firstClickIndex == -1)
                        {
                            firstClickIndex = clickedIndex;
                            firstClickTime = System.currentTimeMillis();
                        }
                        else
                        {
                            if(System.currentTimeMillis() - firstClickTime < 750)
                            {
                                String msg = "plrattemptequip:"+
                                        (clickedIndex + (itemPage * pageXSize * pageYSize));
                                getClientWindow().getClientDomainController().askTheServer(msg);
                                
                                refreshData();
                            }
                            firstClickIndex = -1;
                        }
                    }
                }
                clickedIndex++;
            }
        }
        
        String unequip = "";
        if(e.getSource() == ilBelt)
        {
            if(ilBelt.getClientItem() != null)
            {
                unequip = "belt";
            }
        }
        else if(e.getSource() == ilBoots)
        {
            if(ilBoots.getClientItem() != null)
            {
                unequip = "boots";
            }
        }
        else if(e.getSource() == ilChestArm)
        {
            if(ilChestArm.getClientItem() != null)
            {
                unequip = "chest";
            }
        }
        else if(e.getSource() == ilGloves)
        {
            if(ilGloves.getClientItem() != null)
            {
                unequip = "gloves";
            }
        }
        else if(e.getSource() == ilHelmet)
        {
            if(ilHelmet.getClientItem() != null)
            {
                unequip = "helmet";
            }
        }
        else if(e.getSource() == ilLeftRing)
        {
            if(ilLeftRing.getClientItem() != null)
            {
                unequip = "leftring";
            }
        }
        else if(e.getSource() == ilLeftWeap)
        {
            if(ilLeftWeap.getClientItem() != null)
            {
                unequip = "leftweap";
            }
        }
        else if(e.getSource() == ilLeggings)
        {
            if(ilLeggings.getClientItem() != null)
            {
                unequip = "legs";
            }
        }
        else if(e.getSource() == ilNecklace)
        {
            if(ilNecklace.getClientItem() != null)
            {
                unequip = "necklace";
            }
        }
        else if(e.getSource() == ilRightRing)
        {
            if(ilRightRing.getClientItem() != null)
            {
                unequip = "rightring";
            }
        }
        else if(e.getSource() == ilRightWeap)
        {
            if(ilRightWeap.getClientItem() != null)
            {
                unequip = "rightweap";
            }
        }
        
        if(unequip.length() > 1)
        {
            if(firstClickIndex == -1)
            {
                firstClickIndex = -2;
                firstClickTime = System.currentTimeMillis();
            }
            else
            {
                if(System.currentTimeMillis() - firstClickTime < 750)
                {
                    String msg = "plrunequips:"+unequip;
                    getClientWindow().getClientDomainController().askTheServer(msg);

                    refreshData();
                }
                firstClickIndex = -1;
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
    
    private class ItemLabel extends JPanel implements ActionListener
    {
        private ClientItem ci;
        private JLabel itemNameLabel;
        private JLabel imageLabel;
        private JButton inspectButton;
        
        public ItemLabel()
        {
            this.setLayout(new BorderLayout());
            
            itemNameLabel = new JLabel("");
            this.add(itemNameLabel, BorderLayout.NORTH);
            imageLabel = new JLabel();
            imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
            imageLabel.setVerticalAlignment(SwingConstants.CENTER);
            this.add(imageLabel, BorderLayout.CENTER);
            inspectButton = new JButton("  Inspect ");
            inspectButton.addActionListener(this);
            this.add(inspectButton, BorderLayout.SOUTH);
            
            this.setPreferredSize(new Dimension(100,100));
            this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }
        
        public void setClientItem(ClientItem itan)
        {
            if(itan != null)
            {
                ci = itan;
                itemNameLabel.setText(itan.getItemName());
                imageLabel.setIcon(new ImageIcon(itan.getImage(), itan.getItemDescription()));
                inspectButton.setVisible(true);
            }
            else
            {
                ci = null;
                itemNameLabel.setText("");
                imageLabel.removeAll();
                imageLabel.setIcon(null);
                inspectButton.setVisible(false);
            }
        }
        public ClientItem getClientItem()
        {
            return ci;
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == inspectButton)
            {
                inspect(ci);
            }
        }
    }
}
